package com.code.Array;

import java.util.function.Consumer;

/**
 * Username : YouQiRong
 * Date : 2025/5/17 13:54
 * Class: 动态数组
 */
public class DynamicArray {
    private int size; //逻辑大小
    private int capacity = 8; //容量
    private int[] array = new int[capacity];

    public void addLast(int element) {
        add(size,element);
    }

    public void add(int index,int element){
        if(index >= 0 && index < size){
            System.arraycopy(array,index,
                            array,index+1,size-index);
        }
        array[index] = element;
        size++;
    }

    public int get(int index){ //[0,index)
        return array[index];
    }

    public void forEach(Consumer<Integer> consumer){
        for (int i = 0; i < size; i++) {
            consumer.accept(array[i]);
        }
    }
}
